<template>
  <div class="finish-container">
    <div class="success-icon">✓</div>
    <h1>订单提交成功</h1>
    
    <!-- 订单信息 -->
    <div class="order-info">
      <h2>订单信息</h2>
      <div class="info-item">
        <span class="label">订单编号：</span>
        <span class="value">{{ orderId }}</span>
      </div>
      <div class="info-item">
        <span class="label">下单时间：</span>
        <span class="value">{{ orderTime }}</span>
      </div>
    </div>

    <!-- 收货信息 -->
    <div class="delivery-info">
      <h2>收货信息</h2>
      <div class="info-content">
        <p class="name">{{ orderInfo.address.username }} {{ orderInfo.address.phone }}</p>
        <p class="address">{{ orderInfo.address.address }}</p>
      </div>
    </div>

    <!-- 商品列表 -->
    <div class="order-items">
      <h2>商品信息</h2>
      <div class="items-list">
        <div v-for="item in orderInfo.items" :key="item.id" class="item">
          <img :src="`http://localhost:3005/${item.image}`" :alt="item.title">
          <div class="item-info">
            <h3>{{ item.title }}</h3>
            <p class="item-price">¥{{ item.price.toFixed(1) }} × {{ item.quantity }}</p>
          </div>
          <div class="item-total">¥{{ (item.price * item.quantity).toFixed(1) }}</div>
        </div>
      </div>
    </div>

    <!-- 支付信息 -->
    <div class="payment-info">
      <h2>支付信息</h2>
      <div class="info-item">
        <span class="label">支付方式：</span>
        <span class="value">{{ paymentMethodName }}</span>
      </div>
      <div class="info-item total">
        <span class="label">实付金额：</span>
        <span class="value price">¥{{ orderInfo.finalAmount.toFixed(1) }}</span>
      </div>
    </div>

    <!-- 底部按钮 -->
    <div class="bottom-buttons">
      <button class="primary-btn" @click="router.push('/order')">继续点餐</button>
      <button class="secondary-btn" @click="router.push('/shopping/over')">查看订单</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useCartStore } from '../../stores/cart'

const router = useRouter()
const route = useRoute()
const cartStore = useCartStore()

// 获取路由传递的订单信息
const orderInfo = ref(route.query.orderInfo ? JSON.parse(route.query.orderInfo as string) : null)

// 如果没有订单信息，返回结算页面
if (!orderInfo.value) {
  router.replace('/shopping/settlement')
}

// 生成订单号和下单时间
const orderId = ref(`DD${Date.now()}`)
const orderTime = ref(orderInfo.value?.orderTime ? new Date(orderInfo.value.orderTime).toLocaleString() : new Date().toLocaleString())

// 获取支付方式名称
const paymentMethods = {
  alipay: '支付宝',
  wechat: '微信支付',
  card: '银行卡'
}

const paymentMethodName = computed(() => {
  return orderInfo.value ? (paymentMethods[orderInfo.value.paymentMethod] || '未知支付方式') : ''
})

// 重试提交订单
const retryOrder = () => {
  router.replace('/shopping/settlement')
}
</script>

<style scoped>
.finish-container {
  width: 400px;
  padding: 20px;
  padding-bottom: 100px;
  background-color: #f8f9fa;
  height: 100%;
}

.success-icon {
  text-align: center;
  font-size: 48px;
  color: #42b883;
  margin: 20px 0;
  animation: scaleIn 0.5s ease;
}

h1 {
  text-align: center;
  color: #333;
  font-size: 24px;
  margin-bottom: 30px;
}

h2 {
  font-size: 18px;
  color: #333;
  margin-bottom: 15px;
}

.order-info,
.delivery-info,
.order-items,
.payment-info {
  background: white;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.info-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 14px;
}

.label {
  color: #666;
}

.value {
  color: #333;
  font-weight: 500;
}

.info-content {
  padding: 10px 0;
}

.name {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 5px;
}

.address {
  color: #666;
  font-size: 14px;
}

.items-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.item:last-child {
  border-bottom: none;
}

.item img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 4px;
}

.item-info {
  flex: 1;
  margin: 0 15px;
}

.item-info h3 {
  margin: 0;
  font-size: 14px;
}

.item-price {
  color: #666;
  font-size: 12px;
  margin-top: 5px;
}

.item-total {
  color: #f56c6c;
  font-weight: bold;
}

.total {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #eee;
}

.price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
}

.bottom-buttons {
  margin-top: 20px;
  padding: 15px;
  background: white;
  display: flex;
  gap: 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  width: 370px;
}

.primary-btn,
.secondary-btn {
  flex: 1;
  padding: 12px;
  border-radius: 20px;
  border: none;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.primary-btn {
  background: #42b883;
  color: white;
}

.secondary-btn {
  background: #f5f5f5;
  color: #666;
}

.primary-btn:active {
  background: #3aa876;
}

.secondary-btn:active {
  background: #e8e8e8;
}

@keyframes scaleIn {
  from {
    transform: scale(0);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}
</style>